package main

import (
	config2 "bookStore/config"
	"bookStore/contorller/bookController"
	"bookStore/contorller/fileController"
	"bookStore/contorller/userController"
	model2 "bookStore/model"
	"bookStore/router"
	"github.com/gin-gonic/gin"
	"log"
	"strconv"
)

func init() {
	err := config2.ReadConfiguration("./config/config.yml")
	if err != nil {
		log.Panicln("配置文件读取错误", err.Error())
	}
	config2.ConnectMySql()
	config2.MySql = config2.MySql.AutoMigrate(&model2.Author{}, &model2.Book{}, &model2.User{})
}

func main() {
	defer config2.DisConnectMySql()
	r := gin.Default()
	r.POST("/login", userController.Login)

	userGroup := r.Group("/user", router.UserLoginInterceptor)
	userGroup.GET("/find", userController.FindUserById)
	userGroup.PUT("/add", userController.AddUser)
	userGroup.PUT("/batch/add", userController.BatchAddUser)

	bookGroup := r.Group("/book", router.UserLoginInterceptor)
	bookGroup.PUT("/add", bookController.Put)

	fileGroup := r.Group("/file", router.UserLoginInterceptor)
	fileGroup.POST("/upload", fileController.FileUpload)
	fileGroup.GET("/download", fileController.FileDownload)

	r.Run(":" + strconv.Itoa(config2.GetConfiguration().Server.Port))
}
